const _ = require('lodash');
const foodItemFacade = require('../../app/facade/foodItem.server.facade.js');
module.exports = {
    getFoodItemByName : getFoodItemByName,
    getFoodItemByProducer: getFoodItemByProducer,
    addFoodItem: addFoodItem,
    deleteFoodItemById : deleteFoodItemById,
    getFoodItemList : getFoodItemList
}

function getFoodItemByName(req, res, next) {
    let params = req.params;
    let name = params.name;
    if (_.isEmpty(name)) {
        return next(new Error('food name is empty'));
    } else {
    foodItemFacade.getFoodItemByName(name, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            // return res.json(result);
            res.render('foodItem', result);
        }
    });
    }
}

function getFoodItemByProducer(req, res, next) {
    let body = req.body;
    let producer = body.producer;
    if (_.isEmpty(producer)) {
        return next(new Error('producer name is empty'));
    } else {
    foodItemFacade.getFoodItemByProducer(producer, (error, result) => {
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });
    }
}

function addFoodItem(req,res,next){
    let body = req.body;
    let foodItem = body.foodItem;
    if(_.isEmpty(foodItem)){
        return next(new Error('FoodItem is empty'));
    }else{
        foodItemFacade.addFoodItem(foodItem, (error,result)=>{
            if(error){
                return next(new Error(error));
            }else{
                return res.json(foodItem);
            }
        });
    }
}

function deleteFoodItemById(req,res,next){
    let body = req.body;
    let itemId = body.itemId;
    if(_.isEmpty(itemId)){
        return next(new Error('itemId is empty'));
    }else{
        foodItemFacade.deleteFoodItemById(itemId, (error,result)=>{
            if(error){
                return next(new Error(error));
            }else{
                return res.json(result);
            }
        });
    }
}

function getFoodItemList(req,res,next){
    foodItemFacade.getFoodItemList((error,result)=>{
        if(error){
            return next(new Error(error));
        }else{
            return res.json(result);
        }
    });
}